Downloading humidity data with nasapower

The nasapower package aims at making it quick and easy to automate downloading NASA POWER (NASA Prediction of Worldwide Energy Resource) global meteorology, surface solar energy and climatology data.

Here, we will use the nasapower package to retrieve the relative humidity data for specific countries or for the world.

We have also used the nasapower package to retrieve rainfall data here. The rainfall tutorial includes an introduction on the nasapower package and how its functions work.

Installing nasapower package

We can install the package from CRAN and load it as follows.

install.packages("nasapower")
library(nasapower)

Using get_power() to fetch data

First let us have a look at how to get the daily data for humidity in agriculture. This can be done using the get_power() function.

Fetching daily data for single point

We use get_power() function arguments pars = "RH2M" which means relative Humidity at 2 meters, temporal_average = "DAILY", and longlat equal to a single location.

data_RH <- get_power(community = "AG",
          lonlat = c(134.489563,-25.734968),
          dates = c("2010-09-23","2010-12-23"),
          temporal_average = "DAILY",
          pars = "RH2M")
data_RH %>% datatable(extensions = c('Scroller','FixedColumns'), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = 't',
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))

Fetching daily data for an area

daily_humidity <- get_power(community = "AG",
          lonlat = c(150.5, -28.5 , 153.5, -25.5),
          pars = "RH2M",
          dates = c("2004-09-19","2004-09-29"),
          temporal_average = "DAILY")
daily_humidity %>% datatable(extensions = c('Scroller','FixedColumns'), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = 't',
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))

Fetching climatology data

Global data are obtained by setting community = "AG" and temporal_average = "CLIMATOLOGY".

climate_avg_RH <- get_power(community = "AG",
                         pars = "RH2M",
                         lonlat = "GLOBAL",
                         temporal_average = "CLIMATOLOGY"
)
climate_avg_RH %>% datatable(extensions = c('Scroller','FixedColumns'), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = 't',
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))

Creating spatial objects from get_power()

If you require spatial objects to work with, it is simple to convert the resultant data frame from get_power() to a spatial object using the terra::rast(type = "...").

#getting the shapefiles for the world 
map <- ne_countries(returnclass = "sf")

climate_box <- split(climate_avg_RH,climate_avg_RH$PARAMETER)

climate_box <- lapply(climate_box, function(x){
  x["PARAMETER"] <- NULL
  x
})

climate_box <- lapply(X = climate_box, FUN= as.matrix)

#retrieving the humidity data using the above made climate_box function
relative_humid <- rast(climate_box$RH2M[,c(1:2,15)],
     crs = "+proj=eqc +lat_ts=0 +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs", type = "xyz")

#converting above raster object into a dataframe for mapping 
RH_df <- as.data.frame(relative_humid, xy = TRUE, na.rm = TRUE)
rownames(RH_df) <- c()

#plotting the graph
ggplot() + 
  geom_raster(data = RH_df, aes(x = x, y = y, fill = ANN)) +
  geom_sf(data = map, inherit.aes = FALSE, fill = NA) + 
  scale_fill_viridis()+
  labs(title = "Relative Humidity",
       fill = "Humidity",
       subtitle = "Annual Relative Humidity at various parts of the world")

Using nasapower with large geographical areas

We can plot the relative humidity data for large geographical areas using the shapefiles of different countries, raster cubes and finally plotting them together to show the humidity variation throughout the region.

library("rnaturalearth")
AUS <- ne_states(country = "Australia",
                 returnclass = "sf")
NT <- AUS[AUS$name == "Northern Territory",]
WA <- AUS[AUS$name == "Western Australia",]
#creating raster objects
rasters <- rast(xmin = -180,
     xmax = 180,
     ymin = -90,
     ymax = 90,
     resolution = 0.5)
values(rasters) <- 1:ncell(rasters)
#retrieving only the coordinates from the NT & WA tables 
NT_coord <- crop(rasters,NT)
WA_coord <- crop(rasters,WA)
#getting the coordinated for the first administrative boundary
NT_coord <- mask(NT_coord,vect(NT))
#converting the coordinated into a dataframe with x and y values to map
NT_df <- as.data.frame(NT_coord, xy = TRUE, na.rm = TRUE)
rownames(NT_df) <- c()
#getting the US administrative boundaries shapefile, can alos use the ozmap package for australia shapefile
AUS_map <- geoboundaries("Australia","adm1")
ggplot() + 
  geom_raster(data = NT_df, aes(x = x, y = y, fill = lyr.1))+ 
  geom_sf(data = AUS_map, inherit.aes = FALSE, fill = NA)+
  scale_fill_viridis()+
  theme_minimal()+
  labs(title = "Relative Humidity in Northern Territory,Australia", fill = "Humidity")

WA_coord <- mask(WA_coord,vect(WA))
WA_df <- as.data.frame(WA_coord, xy = TRUE, na.rm = TRUE)
rownames(WA_df) <- c()
ggplot() + 
  geom_raster(data = WA_df, aes(x = x, y = y, fill = lyr.1))+ 
  geom_sf(data = AUS_map, inherit.aes = FALSE, fill = NA)+
  scale_fill_viridis()+
  theme_minimal()+
  labs(title = "Relative Humidity in Western Australia, Australia", fill = "Humidity")

Now let us have a look at the relative humidity monthly data for all the countries in the world:

RH2M <- as.matrix(subset(climate_avg_RH, PARAMETER == "RH2M")[, -3])
RH2M_MONTH <- vector(mode = "list", length = 13)
names(RH2M_MONTH) <- colnames(RH2M)[-c(1:2,13)]
#converting each month into a spatRaster object using the for loop
for (k in colnames(RH2M)[-c(1:2,13)]) {
  RH2M_MONTH[[k]] <- rast(RH2M[, c("LON","LAT",k)],
                        crs = "+proj=eqc +lat_ts=0 +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs",
                        type = "xyz")
  
}
RH2M <- c(rast(RH2M_MONTH))
plot(RH2M)

References